package cn.burnit.view;

import cn.burnit.pojo.User;
import cn.burnit.service.UserService;
import cn.burnit.utils.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录
 */
public class LoginForm extends JFrame {


    private JComboBox comboBox;
    private JLabel title,usernamelab,passwordlab,select;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton submit,updatePwd,regist;
    private UserService service = new UserService();

    public LoginForm() {

        Container container = getContentPane();
        container.setLayout(null);


        submit=new JButton("登录");
        submit.setBounds(20,210,60,20);
        //登录监听
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginByUserAndPwd(e);
            }
        });
        regist=new JButton("注册");
        regist.setBounds(90,210,60,20);
        //跳转到注册界面
        regist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegistForm();
            }
        });

        updatePwd=new JButton("修改密码");
        updatePwd.setBounds(160,210,100,20);
        //更新密码
        updatePwd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UpdatePwdForm();
            }
        });


        title=new JLabel("图书管理系统");
        title.setFont(new Font("宋体", Font.PLAIN, 24));
        title.setBounds(70,30,200,25);
        usernamelab=new JLabel("用户名:");
        usernamelab.setFont(new Font("宋体", Font.PLAIN, 16));
        usernamelab.setBounds(50,80,60,20);
        passwordlab=new JLabel("密码:");
        passwordlab.setFont(new Font("宋体", Font.PLAIN, 16));
        passwordlab.setBounds(50,120,60,20);
        select=new JLabel("选择");
        select.setFont(new Font("宋体", Font.PLAIN, 16));
        select.setBounds(50,160,40,20);


        usernameField=new JTextField();
        usernameField.setBounds(120,80,130,20);
        passwordField=new JPasswordField();
        passwordField.setEchoChar('*');
        passwordField.setBounds(120,120,130,20);

        String[] user=new String[]{"用户","管理员"};
        comboBox=new JComboBox(user);
        comboBox.setBounds(120,160,80,20);
        container.add(title);
        container.add(usernamelab);container.add(usernameField);
        container.add(passwordlab);container.add(passwordField);
        container.add(select);container.add(comboBox);
        container.add(submit);container.add(regist);container.add(updatePwd);


        //container.setBackground(Color.RED);
        setTitle("登录");
        setSize(300,300);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void loginByUserAndPwd(ActionEvent e) {
        String username = this.usernameField.getText();
        String password = new String(this.passwordField.getPassword());
        String is_admin="";

        String selValue = (String) this.comboBox.getSelectedItem();
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"用户名或密码不能为空");
        }else {
            if (selValue.equals("用户")){
                is_admin="0";
                User user = service.login(username, password, is_admin);
                if (user!=null){
                    JOptionPane.showMessageDialog(null,"登录成功");
                    //getUser(user.getUsername());
                    dispose();

                    new UserMainForm();
                }else {
                    JOptionPane.showMessageDialog(null,"账号或密码错误");
                }
            }else {
                is_admin="1";//管理员
                User user = service.login(username, password, is_admin);
                if (user!=null){
                    JOptionPane.showMessageDialog(null,"登录成功");
                    dispose();
                    new RootMainForm();
                }else {
                    JOptionPane.showMessageDialog(null,"账号或面错误");
                }
            }
        }

    }

//    public static void main(String[] args) {
//        new LoginForm();
//    }
}
